// 码农果果蔬
const express = require('express')
const apiRouter = express.Router()
const fs = require('fs')
const writeData = require('../../db/module/writeData')
const readData = require('../../db/module/readData')
const delData = require('../../db/module/delData')
const jwt = require('jsonwebtoken')
const { disposeDatabase } = require('../../models/relevanceDatabase')
const { default: axios } = require('axios')
const secretKey = 'Aa2317770710' //生成token的密钥
const crypto = require('crypto')
const decryptData = require('../../models/decryptData')

// 获取调研列表
apiRouter.post('/getInvestigate_surveyData', async (req, res) => {
  // 获取到客户端通过查询字符串发送到服务器的数据
  try {
    const params = req.body


    let result = await readData(params, 'brogrammer')

    res.send(result)
  } catch (error) {
    res.send({
      code: 50000,
      msg: error
    })
  }
})

// 删除调研结果
apiRouter.post('/deleteInvestigate_survey', async (req, res) => {
  try {
    let result = await delData(req.body, 'brogrammer')
    res.send(result)
  } catch (error) {
    res.send({
      code: 50000,
      msg: error
    })
  }
})

// 添加商品
apiRouter.post('/addCommodity', async (req, res) => {
  try {
    const params = req.body
    let result = await writeData(params, 'commodityList')
    res.send(result)
  } catch (error) {
    res.send({
      code: 50000,
      msg: error
    })
  }
})

// 获取商品列表
apiRouter.post('/getCommodityList', async (req, res) => {
  try {
    const params = req.body
    let result = await readData(params, 'commodityList')

    // 关联数据字典字典项
    let database_res = await disposeDatabase([{ key: 'commodityClassify', dataBaseCode: 'classfiy_mngs' }], result.data)
    result.data = database_res
    res.send(result)
  } catch (error) {
    res.send({
      code: 50000,
      msg: error.message
    })
  }
})

// 删除商品
apiRouter.post('/deleteCommodity', async (req, res) => {
  try {
    let result = await delData(req.body, 'commodityList')
    res.send(result)
  } catch (error) {
    res.send({
      code: 50000,
      msg: error
    })
  }
})

module.exports = apiRouter
